package com.ds.lens.data.service.dao;

import com.ds.lens.data.service.dao.entity.LensMonitorMuteInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;

@Repository
public interface LensMonitorMuteInfoDao {

    String COLUMNS = " id, monitor_id, version, mute, created_time ";

    @Insert({
            "insert into lens_monitor_mute_info (monitor_id, version, mute, created_time )",
            "values (#{monitorId,jdbcType=BIGINT}, #{version,jdbcType=INTEGER}, #{mute}, #{createdTime,jdbcType=TIMESTAMP})"
    })
    int insert(LensMonitorMuteInfo record);

    @Select({
            "select",
            COLUMNS,
            "from lens_monitor_mute_info",
            "where id = #{id,jdbcType=BIGINT}"
    })
    @Results(id = "monitorMuteMap", value = {
            @Result(column = "id", property = "id", jdbcType = JdbcType.BIGINT, id = true),
            @Result(column = "monitor_id", property = "monitorId", jdbcType = JdbcType.BIGINT),
            @Result(column = "version", property = "version", jdbcType = JdbcType.INTEGER),
            @Result(column = "state", property = "state", jdbcType = JdbcType.SMALLINT),
            @Result(column = "target", property = "target", jdbcType = JdbcType.VARCHAR),
            @Result(column = "created_time", property = "createdTime", jdbcType = JdbcType.TIMESTAMP)
    })
    LensMonitorMuteInfo selectByPrimaryKey(Long id);

    @Select({
            "select ",
            COLUMNS,
            "from lens_monitor_mute_info",
            "where monitor_id = #{monitorId} and version = #{version} and target = #{target} order by created_time desc limit 1"
    })
    @ResultMap("monitorMuteMap")
    LensMonitorMuteInfo selectByMonitor(@Param("monitorId") Long monitorId, @Param("version") Integer version);

    @Select({
            "<script>",
            "select ",
            COLUMNS,
            " from lens_monitor_mute_info",
            " where monitor_id = #{monitorId} ",
            "<if test='startTime != null and endTime != null'> and created_time between #{startTime} and #{endTime} </if>",
            " order by created_time asc ",
            "</script>"
    })
    @ResultMap("monitorMuteMap")
    List<LensMonitorMuteInfo> selectListByMonitor(@Param("monitorId") Long monitorId,
                                                  @Param("startTime") Date startTime,
                                                  @Param("endTime") Date endTime);


}
